import { Request, toast } from '../../utils/util.js'
let app = getApp()
let request = new Request()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    amountItems : [1, 20, 100, 500, 2000, 5000],
    amount : 1,
    balance : 0,
  },

  selectAmount (e) {
    this.setData({amount : e.currentTarget.dataset.amount})
  },

  submit () {
    let balance = this.data.balance
    let amount = this.data.amount
    if (amount > this.data.balance) {
      toast('余额不足')
      return
    }
    request.post('wallet/withdrawal', res => {
      if(res.success) {
        toast('提现成功')
        this.setData({ balance: balance - amount})
        wx.navigateBack({
          
        })
      }else{
        toast(res.msg)
      }
    }, { amount: amount }).showLoading()
  },

  onLoad: function (options) {
    if (app.globalData.userInfo.mobile != 1) {
      wx.redirectTo({
        url: '/pages/bindMobile/index'
      })
      return
    }
    request.get('wallet/balance', res => {
      if(res.success) {
        this.setData({balance : res.data.balance})
      }
    })
  }
})